Thread: [Win7] SetWindowsHookEx() as a Windows service - SetThreadDesktop()

  1. #1
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50

    [Win7] SetWindowsHookEx() as a Windows service - SetThreadDesktop()

    I realize that in order to set a global hook the process must have access to the user's desktop. And if a process is running as a Windows service (under the SYSTEM account), it doesn't have access to the desktop.

    My code works perfectly as a user-mode process, but I'm trying to get it to run the same as a Windows service. I have tried to manually gain access to the desktop using this function:
    Code:
    void ConnectToDesktop ()
    {
        HWINSTA hWS;
        HDESK hDT;
    
        // Connect to the window station
        hWS = OpenWindowStation ( "Winsta0", FALSE, MAXIMUM_ALLOWED);
    
        if(!SetProcessWindowStation (hWS))
            printf("SetProcessWindowStation()\n");
    
        //Connect to the desktop
        hDT = OpenDesktop ( "Default", 0, FALSE, MAXIMUM_ALLOWED);
    
        SetThreadDesktop (hDT);
    }
    
    int main(int argc, char** argv)
    {
        ConnectToDesktop();
    
        // SetWindowsHookEx() etc... (WH_KEYBOARD_LL and WH_MOUSE_LL)
    
        return 0;    
    }
    I got this function from this article: Override Windows Creation Parameters with a Win32 Service - CodeProject®
    This may have worked on WinXP (although I have not tested it), however it doesn't seem to work on Windows 7.

    Am I just out of luck because it's Windows 7?

    I have also used CreateProcessAsUser() to run it as a normal user (from the service process), and the hook still does not function.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by C_Sparky View Post
    Am I just out of luck because it's Windows 7?
    Yes... Vista and Win7 will prevent this under most circumstances because it's a primary vector for keyloggers and other trojans to gain control over a users system.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    In XP, services ran in the same user session as the first logged on user (session 0). Opening WinSta0\Default opened the users WinSta0\Default and you could get away with it (assuming the user ticked a relevant box).
    In Vista+ services run in session 0, the first logged on user runs in session 1. Opening winsta0\Default opens the session 0 Winsta0\Default which nobody can see or interact with.

    If you need user input in a service you're doing it wrong. The whole point of them is that they run independent of any users.

  4. #4
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    Thanks for the replies, I will have to look for an alternative solution. And adeyblue don't worry I'm not trying to directly interact with the user from a service.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using C# to implement Windows Service
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 04-06-2008, 09:03 PM
  2. windows service
    By esaptonor in forum Windows Programming
    Replies: 4
    Last Post: 07-01-2007, 02:33 AM
  3. Open an excel file from a windows service under Windows Vista
    By AdrianaLuby in forum C# Programming
    Replies: 1
    Last Post: 06-05-2007, 03:55 AM
  4. writing a windows service
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-01-2004, 06:32 AM
  5. windows service
    By Cbuild in forum Windows Programming
    Replies: 5
    Last Post: 01-06-2004, 07:20 PM